//
//  BHCommonService.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/12.
//  一些可以用用的请求放这边

import UIKit

class BHCommonService: NSObject {

    /**关注 关注类型 1场馆 2赛事 3趣玩 4圈子 5.用户,目前只有用户*/
    static func attentionOrNotService(type: Int, businessId: String, handler: @escaping (Bool, String) -> Void) {
        let manager = BHHTTPClient.init()
        manager.post(url: String.common_attensionOrNot_url, paramters: ["type":type,"businessId":businessId]) { (success, errorMsg, responseObject)  in
            if success == true {
               handler(true,"")
            }else {
                handler(false,errorMsg)
                BHToast.toast(text: errorMsg)
       
            }
        }
    }
    
    /**收藏 类型  1:趣玩 2.场馆/医疗机构 3.赛事*/
    static func collectOrNotService(type: Int, businessId: String, handler: @escaping (Bool, String) -> Void) {
        let manager = BHHTTPClient.init()
        manager.post(url: String.common_collect_url, paramters: ["type":type,"businessId":businessId]) { (success, errorMsg, responseObject)  in
            if success == true {
               handler(true,"")
            }else {
                handler(false,errorMsg)
                BHToast.toast(text: errorMsg)
       
            }
        }
    }
    
    /**圈子的点赞*/
    static func circleLikeOrNot(circleId: String,handler: @escaping (Bool, String) -> Void) {
        let manager = BHHTTPClient.init()
        manager.post(url: String.circle_likeOrNot_url, paramters: ["circleId":circleId]) { (success, errorMsg, responseObject)  in
            if success == true {
               handler(true,"")
            }else {
                handler(false,errorMsg)
                BHToast.toast(text: errorMsg)
       
            }
        }
    }
    
    // 举报
    /**举报类型 (1-评论  2-动态  3.用户)*/
    static func reportService(type: Int,businessId: String,reportReason: [String], reportContent: String, reportImages: [String],handler: @escaping (Bool,String) -> Void) {
        
        var tempDic: [String:Any] = [:]
        tempDic["reportType"] = type
        tempDic["businessId"] = businessId
        tempDic["reportContent"] = reportContent
        let reasonJson = reportReason.toJson()
        let imageJson = reportImages.toJson()
        
        tempDic["reportOptionValue"] = reasonJson
        tempDic["reportPicture"] = imageJson

        
        let manager = BHHTTPClient.init()
        manager.post(url: String.common_report_url, paramters: tempDic) { (success, errorMsg, responseObject)  in
            if success == true {
               handler(true,"")
            }else {
                handler(false,errorMsg)
                BHToast.toast(text: errorMsg)
       
            }
        }
    }
}

// MARK: 获取banner
extension BHCommonService {
    //1首页banner 2场馆banner 3赛事banner
    static func queryBannerService(type: Int, handler: @escaping ([BHCommonBannerModel]) -> Void) {
        let banner = BHHTTPClient.init()
        banner.get(url: String.common_banner_listUrl, paramters: ["type":type]) { (success, errorMsg, responseObject)  in
            var temp: [BHCommonBannerModel] = []
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                
                for dic in result {
                    let model: BHCommonBannerModel = BHCommonBannerModel.init()
                    model.parseDic(dic as! [String: Any])
                    temp.append(model)
                }
            }
            handler(temp)
        }
    }
}

extension BHCommonService {
    // 刷新token
    static func refreshTokenService(handler: @escaping (Bool,Int,String) -> Void) {
        let manager = BHHTTPClient.init()
        manager.get(url: String.login_refresh_token_url, paramters: nil) { (success, errorMsg, responseDic) in
            var code = 200
            if success == true {
                let result = BHJsonTool.decodeStringFromDic(dic: responseDic, key: "result")
                BHMeUserInfoModel.shared.token = result
                BHCache.save_userToken(token: result)
            }else {
                code = BHJsonTool.decodeIntFromDic(dic: responseDic, key: "code")
                if code == 501 { // 失效，刷新
                    
                }
                
                if code == 502 { // 过期，重新登录
                    
                }
                if code == 503 { // 账号已被锁定,请联系管理员
                    
                }
                if code == 504 { //    用户不存在
                    
                }
                if code == 505 { //    非法凭证
                    
                }
            }
            handler(success,code,errorMsg)
        }
    }
}


//MARK: 获取筛选条件
extension  BHCommonService {
    // 查询一级筛选类目，类型 1场馆 2赛事/趣玩
    static func queryTypeOneService(type: Int,handler: @escaping ([BHCommonSortTypeModel]) -> Void) {
        let manager = BHHTTPClient.init()
        manager.get(url: String.common_querySortTypeOne_url, paramters: ["type":type]) { (success, errorMsg, responseDic) in
            var temp: [BHCommonSortTypeModel] = []
            
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseDic, key: "result")
                for itemDic in result {
                    let model = BHCommonSortTypeModel.init()
                    model.parseDic(itemDic as! [String:Any])
                    temp.append(model)
                }
            }
            let allModel = BHCommonSortTypeModel.init()
            allModel.text = "全部"
            allModel.typeId = ""
            
            let childModel = BHCommonSortTypeModel.init()
            childModel.text = "全部"
            childModel.typeId = ""
            
            allModel.childArray = [childModel]
            
            temp.insert(allModel, at: 0)
            
            
            handler(temp)
        }
    }
    
    // 查询二级筛选类目
    static func queryTypeTwoService(parentModel: BHCommonSortTypeModel,handler: @escaping ([BHCommonSortTypeModel]) -> Void) {
        let manager = BHHTTPClient.init()
        manager.get(url: String.common_querySortTypeTwo_url, paramters: ["pid":parentModel.typeId]) { (success, errorMsg, responseDic) in
            var temp: [BHCommonSortTypeModel] = []
            
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseDic, key: "result")
                for itemDic in result {
                    let model = BHCommonSortTypeModel.init()
                    model.parentValue = parentModel.text
                    model.parseDic(itemDic as! [String:Any])
                    temp.append(model)
                }
            }
            
            let allModel = BHCommonSortTypeModel.init()
            allModel.parentId = parentModel.typeId
            allModel.parentValue = parentModel.text
            allModel.text = "全部"
            allModel.typeId = ""
            
            temp.insert(allModel, at: 0)
            handler(temp)
        }
    }
    
    // 根据城市名称查询下属所有区
    static func queryCityAreaService(cityName: String?,handler: @escaping ([BHHomeCityModel]) -> Void) {
        var dic: [String:Any] = [:]
        if cityName == nil {
            dic["cityName"] = "南京市"
        }else {
            dic["cityName"] = cityName!
        }
        let manager = BHHTTPClient.init()
        manager.get(url: String.common_queryCityAllArea_url, paramters: dic) { (success, errorMsg, responseDic) in
            var temp: [BHHomeCityModel] = []
            
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseDic, key: "result")
                for itemDic in result {
                    let model = BHHomeCityModel.init()
                    model.parseDic(itemDic as! [String:Any])
                    temp.append(model)
                }
            }
            let allModel = BHHomeCityModel.init()
            allModel.code = ""
            allModel.name = "全区"
            temp.insert(allModel, at: 0)
            handler(temp)
        }
    }
}

extension BHCommonService {
    static func queryProvinceService(appendAll: Bool, handler: @escaping ([BHHomeCityModel]) -> Void)  {
        let manager = BHHTTPClient.init()
        manager.get(url: String.home_province_listUrl, paramters: nil) { (success, errorMsg, responseObject)  in
            var tempArray: [BHHomeCityModel] = []
            
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                
                for dic in result {
                    let model: BHHomeCityModel = BHHomeCityModel.init()
                    model.parseDic(dic as! [String: Any])
                    tempArray.append(model)
                }
                
                if appendAll == true {
                    let model: BHHomeCityModel = BHHomeCityModel.init()
                    model.code = ""
                    model.parentCode = "-1"
                    model.ifMducg = 0
                    model.name = "全部"
                    tempArray.insert(model, at: 0)
                }
            }
            
            handler(tempArray)
        }
    }
    
    static func queryCityService(provinceModel: BHHomeCityModel, appendAll: Bool,handler: @escaping (BHHomeCityModel) -> Void)  {
        let manager = BHHTTPClient.init()
        manager.get(url: String.home_city_listUrl, paramters: ["parentCode":provinceModel.code]) { (success, errorMsg, responseObject)  in
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                
                for dic in result {
                    let childModel: BHHomeCityModel = BHHomeCityModel.init()
                    childModel.parseDic(dic as! [String: Any])
                    provinceModel.childArray.append(childModel)
                }
                if appendAll == true {
                    let model: BHHomeCityModel = BHHomeCityModel.init()
                    model.code = ""
                    model.parentCode = provinceModel.code
                    model.ifMducg = 0
                    model.name = "全部"
                    provinceModel.childArray.insert(model, at: 0)
                }
            }
            handler(provinceModel)
        }
    }
    
    static func queryAreaService(cityModel: BHHomeCityModel, appendAll: Bool,handler: @escaping (BHHomeCityModel) -> Void) {
        let manager = BHHTTPClient.init()
        manager.get(url: String.common_queryCityAllArea_url, paramters: ["cityName":cityModel.name]) { (success, errorMsg, responseObject)  in
            if success == true {
                let result = BHJsonTool.decodeArrayFromDic(dic: responseObject, key: "result")
                
                for dic in result {
                    let childModel: BHHomeCityModel = BHHomeCityModel.init()
                    childModel.parseDic(dic as! [String: Any])
                    childModel.parentCode = cityModel.code
                    cityModel.childArray.append(childModel)
                }
                if appendAll == true {
                    let model: BHHomeCityModel = BHHomeCityModel.init()
                    model.code = ""
                    model.parentCode = cityModel.code
                    model.ifMducg = 0
                    model.name = "全部"
                    cityModel.childArray.insert(model, at: 0)
                }
            }
            handler(cityModel)
        }
    }
    
}


extension BHCommonService {
    /**
     type 商品类型 1场馆年卡 2教练课程 3趣玩 4.赛事
     businessId  商品id
     payType 1支付宝
     */
    static func createOrder(type: Int,businessId: String,payType: Int, handler: ((Bool,String) -> Void)?) {
        SVProgressHUD.show()
        let manager = BHHTTPClient.init()
        manager.post(url: String.common_createOrder_url, paramters: ["businessType":type,"businessId":businessId,"payType":"1"]) { (success, errorMsg, responseDic) in
            SVProgressHUD.dismiss()
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                let orderStr = BHJsonTool.decodeStringFromDic(dic: result, key: "orderStr")
                handler?(true,orderStr)
                
            }else {
                handler?(false,errorMsg)
            }
        }
        
        
    }
}
